<?php
/**
 * Created by PhpStorm.
 * User: taotao
 * Date: 2019/9/23
 * Time: 15:33
 */

namespace App\Http\Service;


use App\Models\Admin\OrderTempModel;
use App\Models\CompanysModel;
use App\Models\User\UserOrdersModel;
use App\Repositories\OrdersRepository;

class OrdersService
{

    protected $order_repository;

    public function __construct()
    {
        $this->order_repository = new OrdersRepository();
    }

    /**
     * 货单搜索条件
     * @return array
     */
    public function getSearchParame()
    {

        return [
            'searchNum' => [ '所有','1-100件','101-500件','501-1000件','1000件以上' ],
            'searchTime' => [ '所有','7天','8-15天','16天-1个月','1-3个月','3-6个月','6个月以上' ],
            'searchPrice' => [ '所有','5000','5000-1万','1-5万','5-10万','10万以上' ]
        ];

    }

    /**
     * 根据货单搜索条件拼接where条件
     * @return array
     */
    public function orderSqlWhere($num=0,$time=0,$price=0)
    {
        $where = [];
        //处理件数
        switch ($num){
            case 1 :
                $min = 1;$max=100;break;
            case 2 :
                $min = 101;$max=500;break;
            case 3 :
                $min = 501;$max=1000;break;
            case 4 :
                $min = 1000;break;
        }
        if(isset($min) && $min){
            $where[] = ['num','>=',$min];
        }
        if(isset($max) && $max){
            $where[] = ['num','<=',$max];
        }

        //处理时长
        switch ($time){
            case 1 :
                $tmin = $tmax = strtotime("+7 day",time());break;
            case 2 :
                $tmin = strtotime("+8 day",time());$tmax=strtotime("+15 day",time());break;
            case 3 :
                $tmin = strtotime("+16 day",time());$tmax=strtotime("+1 month",time());break;
            case 4 :
                $tmin = strtotime("+1 month",time());$tmax=strtotime("+3 month",time());break;
            case 5 :
                $tmin = strtotime("+3 month",time());$tmax=strtotime("+6 month",time());break;
            case 6 :
                $tmin = strtotime("+6 month",time());break;
        }
        if(isset($tmin) && $tmin){
            $where[] = ['period','>=',$tmin];
        }
        if(isset($tmax) && $tmax){
            $where[] = ['period','<=',$tmax];
        }

        //处理价格
        switch ($price){
            case 1 :
                $pmin = $pmax = 5000;break;
            case 2 :
                $pmin = 5000;$pmax=10000;break;
            case 3 :
                $pmin = 10000;$pmax=50000;break;
            case 4 :
                $pmin = 50000;$pmax=100000;break;
            case 5 :
                $pmin = 100000;break;
        }
        if(isset($pmin) && $pmin){
            $where[] = ['allmoney','>=',$pmin];
        }
        if(isset($pmax) && $pmax){
            $where[] = ['allmoney','<=',$pmax];
        }

        return $where;
    }

    /**
     * 首页货单列表
     * @param $where
     * @param $page
     * @param $pageSize
     * @return array
     */
    public function getBalesOrders($where,$page,$pageSize)
    {
        $data = [];
        $orders = $this->order_repository->getOrders($where,$page,$pageSize);
        foreach ($orders as $order){
            //公司信息
            $com = CompanysModel::where('uid',$order['uid'])->first();
            $val['com_name'] = $com->com_name ?? '';
            $val['com_addr'] = $com->com_address ?? '';
            $val['num'] = $order['num'];
            $val['img'] = $order['img'];
            $val['allmoney'] = $order['allmoney'];
            $val['period'] = $order['period'];
            $data[] = $val;
        }
        return $data;
    }

    /**
     * 发布中的订单
     * @param $uid
     * @param $page
     * @param $pageSize
     * @return array
     */
    public function pushingLists($uid,$page,$pageSize)
    {
        $orderBealArr = [];
        $orders = $this->order_repository->pushingLists($uid,$page,$pageSize);
        if($orders){
            foreach ($orders as $t){
                $item['id'] = $t['id'];
                $item['title'] = $t['name'];
                $item['addtime'] = $t['created_at'] ? date('Y-m-d H:i:s',$t['created_at']) : '';
                $item['exptime'] = $t['period'] ? date('Y-m-d H:i:s',$t['period']) : '';
                $item['img'] = $t['img'];
                $count = OrderTempModel::where(['bale'=>$t['id']])->count();
                $item['company'] = $count ? $count.' 家工厂正在抢单' : '0家工厂正在抢单';
                $orderBealArr[] = $item;
            }
        }
        return $orderBealArr;
    }

    /**
     * 抢单中的订单
     * @param $uid
     * @param $page
     * @param $pageSize
     * @return array
     */
    public function baleLists($uid,$page,$pageSize)
    {
        $orderBealArr = [];
        $orders = $this->order_repository->baleLists($uid,$page,$pageSize);
        if($orders){
            foreach ($orders as $t){
                $item['id'] = $t['bale'];
                $item['company'] = CompanysModel::where('uid',$t['cid'])->value('com_name');
                $count = OrderTempModel::where(['bale'=>$t['bale']])->count();
                $item['num'] = $count ? $count : 0;
                $item['out_trade_no'] = $t['out_trade_no'];
                $item['title'] = $t['order_detail']['name'];
                $item['addtime'] = $t['created_at'] ? date('Y-m-d H:i:s',$t['created_at']) : '';
                $item['exptime'] = $t['order_detail']['period'] ? date('Y-m-d H:i:s',$t['order_detail']['period']) : '';
                $item['img'] = $t['order_detail']['img'];
                $item['order_status'] = $t['status']; //抢单中还是生产中  1未支付，2已支付未抢单，3抢单中，4抢单成功  5抢单失败
                $orderBealArr[] = $item;
            }
        }
        return $orderBealArr;
    }

    /**
     * 生产中的订单
     * @param $uid
     * @param $page
     * @param $pageSize
     * @return array
     */
    public function produceLists($uid,$page,$pageSize)
    {
        $orderBealArr = [];
        $orders = $this->order_repository->produceLists($uid,$page,$pageSize);
        if($orders){
            foreach ($orders as $t){
                $item['id'] = $t['bale'];
                $item['company'] = $t['order_factory']['com_name'];
                $item['out_trade_no'] = $t['order_no'];
                $item['addtime'] = $t['created_at'] ? date('Y-m-d H:i:s',$t['created_at']) : '';
                $item['order_status'] = $t['status']; //抢单中还是生产中  1未支付，2已支付未抢单，3抢单中，4抢单成功  5抢单失败
                $orderBealArr[] = $item;
            }
        }
        return $orderBealArr;
    }

    /**
     * 已完成的订单
     * @param $uid
     * @param $page
     * @param $pageSize
     * @return array
     */
    public function overLists($uid,$page,$pageSize)
    {
        $orderBealArr = [];
        $orders = $this->order_repository->overLists($uid,$page,$pageSize);
        if($orders){
            foreach ($orders as $t){
                $item['id'] = $t['bale'];
                $item['company'] = $t['order_factory']['com_name'];
                $item['out_trade_no'] = $t['order_no'];
                $item['addtime'] = $t['created_at'] ? date('Y-m-d H:i:s',$t['created_at']) : '';
                $item['signtime'] = ''; // todo 后期加上签收时间
                $orderBealArr[] = $item;
            }
        }
        return $orderBealArr;
    }

}